package com.xgg.taiwuinfo;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;

import com.xgg.taiwuinfo.filter.FilterList;
import com.xgg.taiwuinfo.fragement.FilterListFragement;
import com.xgg.taiwuinfo.fragement.GongfaListFragement;
import com.xgg.taiwuinfo.gongfa.base.Gongfa;
import com.xgg.taiwuinfo.gongfa.base.GongfaList;
import com.xgg.taiwuinfo.utils.GongfaType;
import com.xgg.taiwuinfo.utils.Requirement;

public class MainActivity extends AppCompatActivity
{
    GongfaListFragement FragGongfa = new GongfaListFragement();
    FilterListFragement FragFilter = new FilterListFragement();
    private String mCurFilterId = "0";

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener()
    {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item)
        {
            switch (item.getItemId()) {
                case R.id.nav_buildlist:
                    return true;
                case R.id.nav_filterlist:
                    getFragmentManager().beginTransaction().replace(R.id.layout_main, FragFilter).commit();
                    return true;
                case R.id.nav_gongfalist:
                    getFragmentManager().beginTransaction().replace(R.id.layout_main, FragGongfa).commit();
                    return true;
            }
            return false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //部分初始化过程，可能要新开线程来做
        Requirement.InitConst(this);
        Gongfa.InitConst(this);
        GongfaList.Init();
        FilterList.Init(this);
        //初始化过程结束

        BottomNavigationView navigation = findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        navigation.setSelectedItemId(R.id.nav_gongfalist);
    }

    public void NavToGongfaDetail(String id)
    {
        int index = Integer.parseInt(id);
        int type = GongfaList.getList().get(index).mType;
        if ((type & GongfaType.TYPE_CUIPO) > 0)
        {
            type = GongfaType.TYPE_CUIPO;
        }
        Intent intent = new Intent();
        switch (type)
        {
            case GongfaType.TYPE_NEIGONG:
                //点击的内功
                intent.setClass(MainActivity.this,NeigongDetailActivity.class);
                intent.putExtra("id", index);
                startActivity(intent);
                break;
            case GongfaType.TYPE_SHENFA:
                intent.setClass(MainActivity.this,ShenfaDetailActivity.class);
                intent.putExtra("id", index);
                startActivity(intent);
                break;
            case GongfaType.TYPE_JUEJI_HUTI:
                intent.setClass(MainActivity.this,HutiDetailActivity.class);
                intent.putExtra("id", index);
                startActivity(intent);
                break;
            case GongfaType.TYPE_JUEJI_QIQIAO:
                intent.setClass(MainActivity.this,QiqiaoDetailActivity.class);
                intent.putExtra("id", index);
                startActivity(intent);
                break;
            case GongfaType.TYPE_CUIPO:
                intent.setClass(MainActivity.this,CuipoDetailActivity.class);
                intent.putExtra("id", index);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    public void SetCurFilterId(String id)
    {
        mCurFilterId = id;
    }

    public void NavToFilterDetail(String id) {
        int index = Integer.parseInt(id);
        Intent intent = new Intent();
        intent.setClass(MainActivity.this, FilterDetailActivity.class);
        intent.putExtra("id", index);
        startActivity(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater in = getMenuInflater();
        in.inflate(R.menu.mainmenu, menu);
        return true;
    }

     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_filterdetail:
                NavToFilterDetail(mCurFilterId);
                break;
            case R.id.menu_addfilter:
                final EditText et = new EditText(this);
                et.setText("新过滤器");
                et.setMaxLines(1);
                AlertDialog alertDialog = new AlertDialog.Builder(this)
                    .setTitle("编辑名称")
                    .setView(et)
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //按下确定键后的事件
                            String str = et.getText().toString();
                            if (str.length() > 0)
                            {
                                if (FilterList.DupName(str)) {
                                    FilterList.AddTail(str , MainActivity.this);
                                    if (FragFilter.getActivity() != null)
                                        FragFilter.ReloadFilter();
                                    if (FragGongfa.getActivity() != null)
                                        FragGongfa.ReloadFilter();
                                }
                                else {
                                    Toast.makeText(MainActivity.this , "与现有名字重复" , Toast.LENGTH_LONG).show();
                                }
                                //新增一个空过滤器到list中
                            }
                        }
                    })
                    .setNegativeButton("取消",null)
                    .create();
                alertDialog.show();
                break;
            default:
                break;
        }
         return super.onOptionsItemSelected(item);
     }
}
